//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// AnimationOptionsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "AnimationOptionsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAnimationOptionsDlg dialog

IMPLEMENT_DYNCREATE(CAnimationOptionsDlg, CBCGPDialog)

CAnimationOptionsDlg::CAnimationOptionsDlg(CWnd* pParent /*=NULL*/)
	: CBCGPDialog(CAnimationOptionsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAnimationOptionsDlg)
	m_nStyle = -1;
	m_nType = ((int)theApp.m_animationType) - 1;
	m_strDuration = _T("");
	m_bIsPreview = theApp.GetInt(_T("PreviewAnimation"), TRUE);
	m_nDuration = 0;
	//}}AFX_DATA_INIT

	EnableVisualManagerStyle();
}


void CAnimationOptionsDlg::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAnimationOptionsDlg)
	DDX_Control(pDX, IDC_ANIMATION_TYPE, m_wndAnimationType);
	DDX_Control(pDX, IDC_SHOW_ANIMATION, m_wndShowAnimation);
	DDX_Control(pDX, IDC_DURATION, m_wndDuration);
	DDX_Control(pDX, IDC_ANIMATION_STYLE, m_wndAnimationStyles);
	DDX_CBIndex(pDX, IDC_ANIMATION_STYLE, m_nStyle);
	DDX_CBIndex(pDX, IDC_ANIMATION_TYPE, m_nType);
	DDX_Text(pDX, IDC_DURATION_LABEL, m_strDuration);
	DDX_Check(pDX, IDC_PREVIEW_ANIMATION, m_bIsPreview);
	DDX_Slider(pDX, IDC_DURATION, m_nDuration);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAnimationOptionsDlg, CBCGPDialog)
	//{{AFX_MSG_MAP(CAnimationOptionsDlg)
	ON_CBN_SELCHANGE(IDC_ANIMATION_STYLE, OnPreviewAnimation)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_DURATION, OnReleasedcaptureDuration)
	ON_WM_HSCROLL()
	ON_CBN_SELCHANGE(IDC_ANIMATION_TYPE, OnPreviewAnimation)
	ON_BN_CLICKED(IDC_SHOW_ANIMATION, OnShowAnimation)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnimationOptionsDlg message handlers

BOOL CAnimationOptionsDlg::OnInitDialog() 
{
	CBCGPDialog::OnInitDialog();
	
	for (int i = 0; i < (int)theApp.m_arAnimationStyleNames.GetSize(); i++)
	{
		m_wndAnimationStyles.AddString(theApp.m_arAnimationStyleNames[i]);
	}
	
	m_nStyle = (int)theApp.m_animationStyle;
	m_wndDuration.SetRange(500, 2000, TRUE);
	m_nDuration = (int)(1000 * theApp.m_dblChartAnimationTime);
	m_strDuration.Format(_T("%.2f sec"), theApp.m_dblChartAnimationTime);

	UpdateData(FALSE);

	m_wndShowAnimation.EnableWindow(theApp.m_animationStyle != CBCGPChartSeries::BCGPChartAnimationStyle_None);
	m_wndShowAnimation.SetWindowText(_T(""));
	m_wndShowAnimation.SetStdImage(CBCGPMenuImages::IdArowRightLarge, CBCGPMenuImages::ImageBlack, (CBCGPMenuImages::IMAGES_IDS)-1,
		CBCGPMenuImages::ImageLtGray);

	m_wndAnimationType.EnableWindow(CBCGPAnimationManager::IsAnimationSupportedByOS());

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAnimationOptionsDlg::OnPreviewAnimation() 
{
	SaveAnimationOptions();
	
	if (m_bIsPreview)
	{
		AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_ANIMATE_CHART);
	}
}

void CAnimationOptionsDlg::OnReleasedcaptureDuration(NMHDR* /*pNMHDR*/, LRESULT* pResult) 
{
	OnPreviewAnimation();
	*pResult = 0;
}

void CAnimationOptionsDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (pScrollBar->GetSafeHwnd() == m_wndDuration.GetSafeHwnd())
	{
		UpdateData();
		m_strDuration.Format(_T("%.2f sec"), 0.001 * m_nDuration);
		UpdateData(FALSE);

		SaveAnimationOptions();

		return;
	}
	
	CBCGPDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CAnimationOptionsDlg::SaveAnimationOptions()
{
	UpdateData();
	
	theApp.WriteInt(_T("PreviewAnimation"), m_bIsPreview);

	theApp.m_animationStyle = (CBCGPChartSeries::BCGPChartAnimationStyle)m_nStyle;
	theApp.WriteInt(_T("AnimationStyle"), (int)theApp.m_animationStyle);

	theApp.m_animationType = (CBCGPAnimationManager::BCGPAnimationType)(m_nType + 1);
	theApp.WriteInt(_T("AnimationType"), (int)theApp.m_animationType);

	theApp.m_dblChartAnimationTime = 0.001 * m_nDuration;
#ifndef _BCGSUITE_INC_
	theApp.WriteDouble(_T("AnimationTime"), theApp.m_dblChartAnimationTime);
#else
	theApp.WriteInt(_T("AnimationTime"), m_nDuration);
#endif

	m_wndShowAnimation.EnableWindow(theApp.m_animationStyle != CBCGPChartSeries::BCGPChartAnimationStyle_None);

	if (m_pParentEdit != NULL)
	{
		m_pParentEdit->SetParentText(theApp.m_arAnimationStyleNames[m_nStyle]);
	}
}

void CAnimationOptionsDlg::OnShowAnimation() 
{
	AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_ANIMATE_CHART);
}
